#!/bin/bash

# 测试推荐收益提现功能
# 场景: A推荐了B和C，B和C充值并产生质押收益，A获得推荐奖励后申请提现

echo "========================================"
echo "测试推荐收益提现功能"
echo "========================================"
echo ""

# 1. 登录管理员账号
echo "=== 1. 登录管理员账号 ==="
ADMIN_RESPONSE=$(curl -s -X POST http://localhost:3000/api/admin/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "admin",
    "password": "admin123"
  }')

ADMIN_TOKEN=$(echo $ADMIN_RESPONSE | grep -o '"token":"[^"]*' | cut -d'"' -f4)
echo "✅ 管理员Token获取成功"
echo ""

# 2. 注册用户A (推荐人)
echo "=== 2. 注册用户A (推荐人) ==="
USER_A_RESPONSE=$(curl -s -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "idNumber": "991001-1000001",
    "username": "userA",
    "name": "推荐人A",
    "birthDateFirst6": "991001",
    "password": "password123",
    "withdrawalBank": "KB국민은행",
    "withdrawalAccount": "1001-2001-3001",
    "accountHolder": "推荐人A",
    "mobileCarrier": "SKT",
    "mobileNumber": "010-1000-0001",
    "entryCode": "TEST001"
  }')

USER_A_ID=$(echo $USER_A_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
# 推荐码就是username
USER_A_CODE="userA"
echo "✅ 用户A注册成功"
echo "   ID: $USER_A_ID"
echo "   推荐码: $USER_A_CODE"
echo ""

# 3. 注册用户B (被推荐人1)
echo "=== 3. 注册用户B (被推荐人1) ==="
USER_B_RESPONSE=$(curl -s -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d "{
    \"idNumber\": \"991002-1000002\",
    \"name\": \"被推荐人B\",
    \"birthDateFirst6\": \"991002\",
    \"password\": \"password123\",
    \"withdrawalBank\": \"신한은행\",
    \"withdrawalAccount\": \"2001-3001-4001\",
    \"accountHolder\": \"被推荐人B\",
    \"mobileCarrier\": \"KT\",
    \"mobileNumber\": \"010-2000-0002\",
    \"entryCode\": \"TEST001\",
    \"referralCode\": \"$USER_A_CODE\"
  }")

USER_B_ID=$(echo $USER_B_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
USER_B_TOKEN=$(echo $USER_B_RESPONSE | grep -o '"token":"[^"]*' | cut -d'"' -f4)
echo "✅ 用户B注册成功"
echo "   ID: $USER_B_ID"
echo ""

# 4. 注册用户C (被推荐人2)
echo "=== 4. 注册用户C (被推荐人2) ==="
USER_C_RESPONSE=$(curl -s -X POST http://localhost:3000/api/auth/register \
  -H "Content-Type: application/json" \
  -d "{
    \"idNumber\": \"991003-1000003\",
    \"name\": \"被推荐人C\",
    \"birthDateFirst6\": \"991003\",
    \"password\": \"password123\",
    \"withdrawalBank\": \"우리은행\",
    \"withdrawalAccount\": \"3001-4001-5001\",
    \"accountHolder\": \"被推荐人C\",
    \"mobileCarrier\": \"LG U+\",
    \"mobileNumber\": \"010-3000-0003\",
    \"entryCode\": \"TEST001\",
    \"referralCode\": \"$USER_A_CODE\"
  }")

USER_C_ID=$(echo $USER_C_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
USER_C_TOKEN=$(echo $USER_C_RESPONSE | grep -o '"token":"[^"]*' | cut -d'"' -f4)
echo "✅ 用户C注册成功"
echo "   ID: $USER_C_ID"
echo ""

# 5. 用户B申请充值
echo "=== 5. 用户B申请充值 50,000,000 KRW ==="
DEPOSIT_B_RESPONSE=$(curl -s -X POST http://localhost:3000/api/deposit/request \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $USER_B_TOKEN" \
  -d '{
    "amount": 50000000
  }')

DEPOSIT_B_ID=$(echo $DEPOSIT_B_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
echo "✅ 用户B充值申请ID: $DEPOSIT_B_ID"
echo ""

# 6. 管理员批准用户B充值 (实际存入500 NEAR)
echo "=== 6. 管理员批准用户B充值 (500 NEAR) ==="
curl -s -X POST http://localhost:3000/api/admin/deposit/${DEPOSIT_B_ID}/process \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -d '{
    "action": "confirm",
    "actualNearAmount": 500,
    "note": "测试充值 - 用户B"
  }' > /dev/null
echo "✅ 用户B充值已批准"
echo ""

# 7. 用户C申请充值
echo "=== 7. 用户C申请充值 30,000,000 KRW ==="
DEPOSIT_C_RESPONSE=$(curl -s -X POST http://localhost:3000/api/deposit/request \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $USER_C_TOKEN" \
  -d '{
    "amount": 30000000
  }')

DEPOSIT_C_ID=$(echo $DEPOSIT_C_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
echo "✅ 用户C充值申请ID: $DEPOSIT_C_ID"
echo ""

# 8. 管理员批准用户C充值 (实际存入300 NEAR)
echo "=== 8. 管理员批准用户C充值 (300 NEAR) ==="
curl -s -X POST http://localhost:3000/api/admin/deposit/${DEPOSIT_C_ID}/process \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $ADMIN_TOKEN" \
  -d '{
    "action": "confirm",
    "actualNearAmount": 300,
    "note": "测试充值 - 用户C"
  }' > /dev/null
echo "✅ 用户C充值已批准"
echo ""

# 9. 查看用户B和C的质押信息
echo "=== 9. 查看用户质押信息 ==="
echo "用户B质押信息:"
curl -s http://localhost:3000/api/staking/info \
  -H "Authorization: Bearer $USER_B_TOKEN" | grep -o '"stakedAmount":[0-9.]*' || echo "查询失败"

echo "用户C质押信息:"
curl -s http://localhost:3000/api/staking/info \
  -H "Authorization: Bearer $USER_C_TOKEN" | grep -o '"stakedAmount":[0-9.]*' || echo "查询失败"
echo ""

# 10. 触发每日收益计算
echo "=== 10. 触发每日收益计算 ==="
curl -s -X POST http://localhost:3000/api/admin/test/calculate-daily-rewards \
  -H "Authorization: Bearer $ADMIN_TOKEN" > /dev/null
echo "✅ 每日收益计算完成"
echo ""

# 11. 触发3次收益发放 (每次会同时发放推荐奖励)
echo "=== 11. 触发收益发放 (3轮) ==="
for i in 1 2 3; do
  echo "   第 $i 次发放..."
  curl -s -X POST http://localhost:3000/api/admin/test/distribute-rewards \
    -H "Authorization: Bearer $ADMIN_TOKEN" > /dev/null
  sleep 1
done
echo "✅ 收益发放完成 (3轮)"
echo ""

# 12. 查看用户A的推荐收益
echo "=== 12. 查看用户A的推荐收益 ==="
USER_A_LOGIN=$(curl -s -X POST http://localhost:3000/api/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "idNumber": "991001-1000001",
    "password": "password123"
  }')

USER_A_TOKEN=$(echo $USER_A_LOGIN | grep -o '"token":"[^"]*' | cut -d'"' -f4)

echo "用户A的推荐收益详情:"
curl -s http://localhost:3000/api/referral/earnings \
  -H "Authorization: Bearer $USER_A_TOKEN" | jq '.data.stakingEarnings'
echo ""

echo "用户A的质押信息 (lockedAmount = 可用推荐收益):"
curl -s http://localhost:3000/api/staking/info \
  -H "Authorization: Bearer $USER_A_TOKEN" | jq '{stakedAmount, lockedAmount, totalRewardsNear}'
echo ""

# 13. 用户A申请提现推荐收益
echo "=== 13. 用户A申请提现推荐收益 ==="
echo "假设NEAR价格为100,000 KRW，申请提现 50,000 KRW"
WITHDRAW_RESPONSE=$(curl -s -X POST http://localhost:3000/api/referral/withdraw/request \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $USER_A_TOKEN" \
  -d '{
    "amountKrw": 50000
  }')

echo "$WITHDRAW_RESPONSE" | jq '.'

WITHDRAW_ID=$(echo $WITHDRAW_RESPONSE | grep -o '"id":[0-9]*' | head -1 | cut -d':' -f2)
if [ -n "$WITHDRAW_ID" ]; then
  echo "✅ 提现申请成功，申请ID: $WITHDRAW_ID"
else
  echo "⚠️  提现申请可能失败，请查看上方响应信息"
fi
echo ""

# 14. 查看提现申请记录
echo "=== 14. 查看提现申请记录 ==="
curl -s http://localhost:3000/api/referral/withdraw/history \
  -H "Authorization: Bearer $USER_A_TOKEN" | jq '.data.records'
echo ""

# 15. 管理员查看所有推荐提现申请
echo "=== 15. 管理员查看所有推荐提现申请 ==="
curl -s http://localhost:3000/api/admin/referral/withdraw-list \
  -H "Authorization: Bearer $ADMIN_TOKEN" | jq '.data.records[] | {id, userId, amountKrw, amountNear, status, createdAt}'
echo ""

echo "========================================"
echo "测试完成!"
echo "========================================"
echo ""
echo "📋 测试总结:"
echo "1. ✅ 创建了3个用户 (A为推荐人, B和C为被推荐人)"
echo "2. ✅ 用户B充值 500 NEAR, 用户C充值 300 NEAR"
echo "3. ✅ 触发收益发放，A应该获得B和C收益的10%推荐奖励"
echo "4. ✅ 用户A申请提现推荐收益"
echo "5. ✅ 可以在管理端查看和处理提现申请"
echo ""
echo "🔍 下一步验证:"
echo "- 在前端登录用户A账号 (991001-1000001 / password123)"
echo "- 查看账户页面的推荐收益卡片"
echo "- 验证'누적'字段显示的是历史累计总额"
echo "- 验证'금일'字段显示的是今天的收益"
echo "- 验证'출금 가능한 수익금'显示的是可提取余额"
echo ""
